package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IContentBaseFeeService;
import com.wondertek.poms.core.service.IEquityNodeService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.service.IProductService;
import com.wondertek.poms.dao.po.ContentBaseFee;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.Product;
import com.wondertek.poms.service.vo.BaseChargeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author evan0213
 * @description 商品包计费
 */
@RestController
@RequestMapping("prd-info-charge")
public class PrdInfoChargeController {

	@Autowired
	private IContentBaseFeeService contentBaseFeeService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IPrdInfoService prdInfoService;
	@Autowired
	private IEquityNodeService equityNodeService;

	@PostMapping("/add-base-charge")
	public ResultBean addBaseCharge(@RequestBody BaseChargeVo baseChargeVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(baseChargeVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		String productId = baseChargeVo.getProductId();
		String equityNodeId = baseChargeVo.getEquityNodeId();
		if (StringUtils.isBlank(productId) && StringUtils.isBlank(equityNodeId)) {
			return ResultBean.error("计费ID、权益节点编码必须选择一个");
		}
		if (StringUtils.isNotBlank(productId) && StringUtils.isNotBlank(equityNodeId)) {
			return ResultBean.error("计费ID、权益节点编码只能选择一个");
		}
		if (StringUtils.isNotBlank(equityNodeId) && StringUtils.isNotBlank(baseChargeVo.getChannelPackageId())) {
			return ResultBean.error("权益节点不支持渠道包计费配置");
		}
		Long prdInfoId = baseChargeVo.getPrdInfoId();
		PrdInfo prdinfo = prdInfoService.findByPrdInfoId(prdInfoId);
		if (StringUtils.isNotBlank(equityNodeId)) {
			String equityIds = prdinfo.getEquityId();
			if (StringUtils.isNotBlank(equityIds)) {
				equityIds = equityIds + "," + equityNodeId;
				prdinfo.setEquityId(equityIds);
			} else {
				prdinfo.setEquityId(equityNodeId);
			}
			prdInfoService.save(prdinfo);
		} else if (StringUtils.isNotBlank(productId) && StringUtils.isNotBlank(baseChargeVo.getChannelPackageId())) {
			Product product = productService.getByProductId(productId);
			ContentBaseFee baseFee = new ContentBaseFee();
			baseFee.setType(1);
			baseFee.setPomsId(prdInfoId);
			baseFee.setProductId(productId);
			baseFee.setPrice(product.getPrice());
			baseFee.setChannelPackageId(Long.valueOf(baseChargeVo.getChannelPackageId()));
			if ("14".equals(product.getChargeMode())) {
				baseFee.setOrderFlag("*T");
			} else {
				baseFee.setOrderFlag(product.getChargeMode());
			}
			contentBaseFeeService.save(baseFee);

		} else {
			Product product = productService.getByProductId(productId);
			if ("0".equals(product.getChargeMode())) {
				String monthType = prdinfo.getMonthType();
				if (StringUtils.isNotBlank(monthType)) {
					monthType = monthType + "," + productId;
				} else {
					monthType = productId;
				}
				prdinfo.setMonthType(monthType);
			} else if ("1".equals(product.getChargeMode())) {
				String numberType = prdinfo.getNumberType();
				if (StringUtils.isNotBlank(numberType)) {
					numberType = numberType + "," + productId;
				} else {
					numberType = productId;
				}
				prdinfo.setMonthType(numberType);
			} else {
				String freeType = prdinfo.getFreeType();
				if (StringUtils.isNotBlank(freeType)) {
					freeType = freeType + "," + productId;
				} else {
					freeType = productId;
				}
				prdinfo.setMonthType(freeType);
			}
			prdInfoService.save(prdinfo);
		}

		return ResultBean.ok();
	}

	/*@GetMapping("/list")
	public ResultBean list(Long prdInfoId) {
		PrdInfo prdInfo = prdInfoService.findByPrdInfoId(prdInfoId);
		if (StringUtils.isNotBlank(prdInfo.getEquityId())) {

		}

		return ResultBean.ok();
	}*/


}
